//
//  JavaSimpleDateFormat.m
//
//  Created by GaoShin on 2020/11/11.
//

#import "JavaSimpleDateFormat.h"

@implementation JavaSimpleDateFormat{
    NSString * datePattern;
    NSLocale * locale;
    NSTimeZone * timeZone;
}

- (instancetype) initWithDatePattern:(NSString * _Nonnull) pattern{
    self = [super init];
    if(self){
        self -> datePattern = pattern;
        self -> locale = [NSLocale currentLocale];
        self -> timeZone     = [NSTimeZone defaultTimeZone];
    }
    return self;
}

//zh_CN en_US etc...
- (instancetype) initWithDatePattern:(NSString * _Nonnull) pattern localeString:(NSString *) locale{
    self = [super init];
    if(self){
        self -> datePattern  = pattern;
        self -> locale = [[NSLocale alloc] initWithLocaleIdentifier:locale];
        self -> timeZone     = [NSTimeZone defaultTimeZone];
    }
    return self;
}

- (instancetype) initWithDatePattern:(NSString * _Nonnull) pattern withLocale:(NSLocale *) locale{
    self = [super init];
    if(self){
        self -> datePattern = pattern;
        self -> locale      = locale;
        self -> timeZone    = [NSTimeZone defaultTimeZone];
    }
    return self;
}

- (instancetype) initWithDatePattern:(NSString * _Nonnull) pattern withLocale:(NSLocale *) locale timeZone:(NSTimeZone *) timeZone{
    self = [super init];
    if(self){
        self -> datePattern = pattern;
        self -> locale      = locale;
        self -> timeZone    = timeZone;
    }
    return self;
}

- (NSString * _Nonnull) format:(JavaDate *) date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:datePattern];
    [dateFormatter setLocale:locale];
    [dateFormatter setTimeZone:timeZone];
    return [dateFormatter stringFromDate:[date getDate]];
}

- (NSString * _Nonnull) formatNSDate:(NSDate *) date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:datePattern];
    [dateFormatter setLocale:locale];
    [dateFormatter setTimeZone:timeZone];
    return [dateFormatter stringFromDate:date];
}

- (JavaDate *) parse:(NSString * _Nonnull) formattedDateString{
    return [[JavaDate alloc] initWithNSDate:[self parseNSDate:formattedDateString]];
}

- (NSDate *) parseNSDate:(NSString * _Nonnull) formattedDateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:datePattern];
    [dateFormatter setLocale:locale];
    [dateFormatter setTimeZone:timeZone];
    NSDate *retdate = [dateFormatter dateFromString:formattedDateString];
    return retdate;
}

- (NSString * _Nonnull) getDatePattern{
    return datePattern;
}

- (void) setDatePattern:(NSString *) pattern{
    self->datePattern = pattern;
}

-(NSLocale * _Nonnull) getNSLocale{
    return locale;
}
-(NSTimeZone * _Nonnull) getNSTimeZone{
    return timeZone;
}

-(void) setNSLocale:(NSLocale *) locale{
    self->locale = locale;
}
-(void) setNSTimeZone:(NSTimeZone *) timeZone{
    self->timeZone = timeZone;
}
@end
